package cc.linker.steplink.crm.merchanth5.controller;

import cc.linker.steplink.crm.commons.Constants;
import cc.linker.steplink.crm.commons.domain.WxUser;
import cc.linker.steplink.crm.merchanth5.commons.RequestFilter;
import cc.linker.steplink.crm.merchanth5.service.MaiBaoLaService;
import cc.linker.steplink.crm.merchanth5.service.ZsGoodsService;
import cc.linker.steplink.crm.response.GlobalException;
import cc.linker.steplink.crm.response.ResponseCode;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/h5/mbl")
public class MaiBaoLaController {

    @Autowired
    private MaiBaoLaService maiBaoLaService;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RequestFilter requestFilter;
    @Autowired
    private ZsGoodsService zsGoodsService;

    /**
     * 商品新增编辑
     *
     * @param goodsBanner  商品图片(数组)5
     * @param goodsContent 商品详情6
     * @param goodsCount   商品数量3
     * @param goodsCover   商品封面图
     * @param goodsFreight 商品运费4
     * @param goodsId      商品id,编辑时传
     * @param goodsName    商品名称1
     * @param moneyAmount  商品价格2
     * @param rebate       返利7
     * @param rebateRf     推荐返利
     */
    @PostMapping("/saveGoods")
    public void saveGoods(@RequestBody Map<String, Object> map, HttpServletRequest request) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        map.put("orgId", orgId);
        map.put("customerId", customerId);
        maiBaoLaService.saveGoods(map);
    }

    /**
     * 商品详情
     * 又要给step用又要给懂小姐用，懂小姐还要去除登录验证的
     * 所以request中没有wxUser
     *
     * @param goodsId 商品id
     */
    @GetMapping("/getGoods")
    public Map getGoods(HttpServletRequest request, String goodsId, @RequestParam(defaultValue = "step") String source) throws GlobalException {
        String customerId = "";
        String orgId = "";
        WxUser wxUser = requestFilter.getWxUserNotLogin(request);
        if (wxUser != null) {
            customerId = wxUser.getCustomerId();
            orgId = wxUser.getOrgId();
        }
        boolean flag = !"step".equals(source);
        return maiBaoLaService.getGoods(customerId, goodsId, orgId, flag, source);
    }

    /**
     * 商品详情
     * 专门给H5获取商品详情页用
     * 所以request中没有wxUser
     *
     * @param goodsId 商品id
     */
    @GetMapping("/getGoodsInfo")
    public Map getGoodsInfo(HttpServletRequest request,
                            String goodsId,
                            @RequestParam(defaultValue = "step") String source,
                            @RequestParam(defaultValue = "") String shareCustomerId,
                            @RequestParam(defaultValue = "1") Integer shareType
    ) throws GlobalException {
        WxUser wxUser = requestFilter.getWxUser(request);
        zsGoodsService.setGoodsShareLog(Long.parseLong(goodsId),shareCustomerId,shareType,wxUser.getCustomerId());
        return maiBaoLaService.getGoodsInfo(wxUser.getCustomerId(), goodsId, wxUser.getOrgId());
    }

    /**
     * 商品详情
     * 专门给H5获取商品详情页用
     * 所以request中没有wxUser
     *
     * @param goodsId 商品id
     */
    @GetMapping("/getSecGoodsLimit")
    public Map getSecGoodsLimit(HttpServletRequest request,
                            String goodsId
    ) throws GlobalException {
        WxUser wxUser = requestFilter.getWxUser(request);
        return maiBaoLaService.getSecGoodsLimit(wxUser.getCustomerId(), goodsId, wxUser.getOrgId());
    }

    /**
     * 商品列表
     */
    @PostMapping("/list")
    public Object list(HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        return maiBaoLaService.getGoodsList(customerId);
    }

    /**
     * 商品上下架
     *
     * @param goodsId 商品id
     */
    @GetMapping("/updateStatus")
    public void updateStatus(HttpServletRequest request, String goodsId) throws GlobalException {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        maiBaoLaService.updateStatus(customerId, goodsId);
    }

    /**
     * 开通麦爆啦之后显示分享者name和累计赚得佣金
     */
    @GetMapping("/sharerInfoAfterMbl")
    public Object sharerInfoAfterMbl(HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String customerId = wxUser.getCustomerId();
        return maiBaoLaService.getRecommenderInfo(customerId);
    }

    /**
     * 商品交易成功之后显示分享者name和累计赚得佣金
     */
    @GetMapping("/sharerInfoAfterGoods")
    public Object sharerInfoAfterGoods(String orderSn) {
        return maiBaoLaService.getSharerInfoAfterGoods(orderSn);
    }


    /**
     * 联系我们信息
     *
     * @return
     */
    @GetMapping("/linkinfo")
    public Map<String, Object> linkInfo(String goodsId) throws GlobalException {
        return maiBaoLaService.getMerchantDetailById(goodsId);
        }

    /**
     * 申请售后信息
     *
     * @return
     */
    @GetMapping("/serviceinfo")
    public Map<String, Object> serviceInfo(String goodsId) throws GlobalException {
        Map<String, Object> param = maiBaoLaService.getBelongByGoodsId(goodsId);
        if (param == null) {
            throw GlobalException.build("商家信息不存在");
        }
        return maiBaoLaService.serviceInfo(param);
    }

    /**
     * 获取商家名称 兼容大麦 联盟商家
     *
     * @param customerId
     * @param request
     * @return
     */
    @GetMapping("/getshopname")
    public Map<String, Object> getShopName(
            @RequestParam(value = "customerId", required = false) String customerId,
            HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String type = "2";
        if (StringUtils.isEmpty(customerId)) {
            // 大麦
            type = "1";
        }
        return maiBaoLaService.getShopName(orgId, customerId, type);
    }

    /**
     * 获取加盟代理和推广员 审核开关
     *
     * @param request
     * @return
     */
    @GetMapping("/getcheckswitch")
    public Map<String, Object> getCheckSwitch(HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        return maiBaoLaService.getCheckSwitch(orgId);
    }


    /**
     * 提交推广员表单 提交加盟代理表单
     * name
     * phone
     * company
     * inviter
     * type  agent 推广员  promoter 加盟代理
     *
     * @param request
     * @return
     */
    @PostMapping("/addcheckinfo")
    public void addCheckInfo(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        if (!StringUtils.isEmpty(map.get("pid")) && customerId.equals(map.get("pid").toString())) {
            throw GlobalException.build("请勿扫自己的二维码");
        }
        map.put("orgId", orgId);
        map.put("customerId", customerId);
        maiBaoLaService.addCheckInfo(map);
    }

    /**
     * 买家退款详情页
     *
     * @param recId
     * @param request
     * @return
     */
    @GetMapping("/refundinfo")
    public Map<String, Object> refundInfo(
            @RequestParam(value = "recId") String recId,
            HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        Map<String, Object> param = new HashMap<>();
        param.put("recId", recId);
        param.put("orgId", orgId);
        param.put("customerId", customerId);
        return maiBaoLaService.refundInfo(param);
    }

    /*
     * 撤销申请
     * orderId
     * goodsId
     */
    @PostMapping("/cancelrefund")
    public void cancelRefund(
            @RequestBody Map<String, Object> param,
            HttpServletRequest request
    ) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        param.put("orgId", orgId);
        param.put("customerId", customerId);
        maiBaoLaService.cancelRefund(param);
    }

    /**
     * 添加退货快递信息
     * expressName
     * expressCode
     * expressRemark
     * expressImages
     * protectCode
     */
    @PostMapping("/addrefundexpress")
    public void addRefundExpress(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        map.put("orgId", orgId);
        map.put("customerId", customerId);
        maiBaoLaService.addRefundExpress(map);
    }

    /**
     * h5 添加协商记录
     * recId
     * protectCode
     * consultContent
     * consultImages
     *
     * @param map
     * @param request
     */
    @PostMapping("/addconsult")
    public void addConsult(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        map.put("orgId", orgId);
        map.put("customerId", customerId);
        map.put("consultStatus", "买家留言");
        maiBaoLaService.addConsult(map);
    }

    /**
     * 协商记录列表
     *
     * @param protectCode
     * @param request
     * @return
     */
    @GetMapping("/consultlist")
    public Map<String, Object> consultList(
            @RequestParam(value = "protectCode") String protectCode,
            HttpServletRequest request
    ) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        String orgId = wxUser.getOrgId();
        String customerId = wxUser.getCustomerId();
        Map<String, Object> param = new HashMap<>();
        param.put("orgId", orgId);
        param.put("customerId", customerId);
        param.put("protectCode", protectCode);
        return maiBaoLaService.consultList(param);
    }

    /**
     * 商品限购判断
     *
     * @param goodsId 商品id，多个用逗号拼接
     */
    @GetMapping("/quota")
    public Map<String, Object> quota(String goodsId, HttpServletRequest request) {
        WxUser wxUser =  requestFilter.getWxUser(request);
        Integer quota = maiBaoLaService.quota(goodsId, wxUser.getCustomerId());
        Map<String, Object> map = new HashMap<>();
        map.put("quota", quota);
        return map;
    }

    /**
     * 根据商品id获取商品的赠品
     */
    @GetMapping("/getExercise")
    public List<Map<String, Object>> getExercise(String goodsId) {
        return maiBaoLaService.getExerciseByGoodsId(goodsId);
    }

    /**
     * 退款进程 动态
     * @param recId
     * @return
     */
    @GetMapping("/getrefundinfolist")
    public List<Map<String, Object>> getRefundInfoList(Integer recId) {
        return maiBaoLaService.getRefundInfoList(recId);
    }
}
